﻿namespace OrderServiceLevel1.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using OrderServiceLevel1.Model;

    public class MemoryRepository : IRepository
    {
        private Dictionary<int, Order> repository = new Dictionary<int, Order>();

        public List<Order> Orders
        {
            get
            {
                return this.repository.Values.OrderBy(x => x.Id).ToList();
            }
        }

        public void AddOrder(Order order)
        {
            Order lastOrder = this.Orders.LastOrDefault();

            var id = lastOrder == null ? 1 : lastOrder.Id + 1;

            order.Id = id;

            this.repository.Add(id, order);
        }

        public virtual Order RetrieveOrder(int id)
        {
            return this.repository[id];
        }

        public void RemoveOrder(int id)
        {
            this.repository.Remove(id);
        }

        public void UpdateOrder(Order order)
        {
            if (this.repository.ContainsKey(order.Id))
            {
                this.repository[order.Id] = order;
            }
        }
    }
}